#pragma once
#include <juce_gui_extra/juce_gui_extra.h>

// 基础节点类
class NodeBase : public juce::Component
{
public:
    NodeBase(const juce::String& labelText);
    ~NodeBase() override = default;

    void paint(juce::Graphics& g) override;
    void mouseDown(const juce::MouseEvent& e) override;
    void mouseDrag(const juce::MouseEvent& e) override;
    void mouseMove(const juce::MouseEvent& e) override;
    void mouseUp(const juce::MouseEvent& e) override;
    void mouseExit(const juce::MouseEvent& e) override;
    void resized() override;

    void setSelected(bool s) { selected = s; repaint(); }
    bool isSelected() const { return selected; }
    float getTitleBarHeight() const { return title_bar_height; }

    void setNodePos(juce::Point<float> p) { nodePos = p; setTopLeftPosition((int)p.x, (int)p.y); }
    juce::Point<float> getNodePos() const { return nodePos; }

    juce::Point<float> getConnector(int idx) const; // 0:左 1:上 2:右 3:下
    int getConnectorAt(const juce::Point<float>& mousePos) const;

    // 获取所有4个连接点的全局坐标
    std::vector<juce::Point<float>> getAllConnectorGlobal() const
    {
        std::vector<juce::Point<float>> pts;
        for (int i = 0; i < 4; ++i)
            pts.push_back(getConnector(i) + getPosition().toFloat());
        return pts;
    }

    juce::String label;

protected:
    const float connectorRadius = 0.0f; // 连接点半径
    bool selected = false;
    bool connectorHover = false;
    juce::Point<float> nodePos;
    juce::Point<float> dragOffset;
    int width = 120, height = 80;
    const float title_bar_height = 20.0f;
    bool isDragging = false; // 新增：是否正在拖动
    float alpha = 0.7f;
};